package com.xhwl.logistics.strategy.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.converter.mapstruct.VehicleCarThirdConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleCardThirdConverter;
import com.xhwl.logistics.dto.http.CarMonthInfoChuangXiangDTO;
import com.xhwl.logistics.dto.http.CardTypeChuangXiangDTO;
import com.xhwl.logistics.entity.VehicleCar;
import com.xhwl.logistics.entity.VehicleCard;
import com.xhwl.logistics.entity.VehicleParkingLot;
import com.xhwl.logistics.service.business.VehicleCommonBusinessService;
import com.xhwl.logistics.service.impl.VehicleCarServiceImpl;
import com.xhwl.logistics.service.impl.VehicleCardServiceImpl;
import com.xhwl.logistics.service.impl.VehicleParkingLotServiceImpl;
import com.xhwl.logistics.service.impl.http.ChuangXiangHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotSyncHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 同步创享车卡类型、车辆列表和黑名单列表
 */
@Slf4j
//@Service
public class VehicleParkingLotChuangXiangSyncHandlerImpl implements IVehicleParkingLotSyncHandler {

    @Autowired
    private VehicleCommonBusinessService vehicleCommonBusinessService;

    @Autowired
    private ChuangXiangHttpServiceImpl chuangXiangHttpService;

    @Autowired
    private VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    private VehicleCardThirdConverter vehicleCardThirdConverter;

    @Autowired
    private VehicleCardServiceImpl vehicleCardService;

    @Autowired
    private VehicleCarServiceImpl vehicleCarService;

    @Autowired
    private VehicleCarThirdConverter vehicleCarThirdConverter;

    private static final String defaultAccount = "xxljob";

    private static final Integer noCardType = 99;

    /**
     * 同步创享车卡类型
     * @param vehicleIntegratedConfigBO
     * @param vehicleSyncBO
     */
    @Override
    public void syncCardRule(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        log.info("Chuang Xiang Card sync start!");
        Project project = vehicleCommonBusinessService.getProjectById(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId());
        if(project==null){
            return;
        }
        List<CardTypeChuangXiangDTO> cardTypeChuangXiangDTOS = chuangXiangHttpService.getParkCarType(vehicleIntegratedConfigBO);
        LinkedList<VehicleCard> vehicleCardListFromThird = new LinkedList<>();
        VehicleParkingLot vehicleParkingLot = null;

        if(!cardTypeChuangXiangDTOS.isEmpty()){
            vehicleParkingLot = vehicleParkingLotService.getOne(new LambdaQueryWrapper<VehicleParkingLot>().eq(
                    VehicleParkingLot::getProjectId, project.getId()
            ).eq(
                    VehicleParkingLot::getParkId, vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getParkId()
            ));
            if(vehicleParkingLot==null){
                throw new BusinessException("创享，通过第三方车场id查询不到车场信息！");
            }
            for (CardTypeChuangXiangDTO cardTypeChuangXiangDTO : cardTypeChuangXiangDTOS) {
                VehicleCard vehicleCard = vehicleCardThirdConverter.chuangXiangtoVehicleCard(cardTypeChuangXiangDTO, project.getId(), project.getName(), vehicleParkingLot.getId(),
                        vehicleParkingLot.getParkingLotName(), defaultAccount, defaultAccount);
                vehicleCardListFromThird.add(vehicleCard);
            }
        }

        List<VehicleCard> vehicleCardListFromCent = vehicleCardService.list(new LambdaQueryWrapper<VehicleCard>().eq(
                VehicleCard::getProjectId, project.getId()
        ).eq(
                Objects.nonNull(vehicleParkingLot),VehicleCard::getParkingLotId, vehicleParkingLot.getId()
        ));

        vehicleCommonBusinessService.syncCardType(vehicleCardListFromCent,vehicleCardListFromThird);
        log.info("Chuang Xiang Card sync end");
    }

    /**
     * 同步创享的车辆信息
     */
    @Override
    public void syncCarList(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        log.info("Chuang Xiang Car sync start!");
        Project project = vehicleCommonBusinessService.getProjectById(vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId());
        if(project==null){
            return;
        }
        List<CarMonthInfoChuangXiangDTO> carMonthInfoChuangXiangDTOList = chuangXiangHttpService.getParkingMonthCarInfo(vehicleIntegratedConfigBO);
        List<VehicleCar> vehicleCarListFromThird = new LinkedList<>();
        List<VehicleCard> vehicleCardList = new LinkedList<>();
        VehicleParkingLot vehicleParkingLot = null;

        if(!carMonthInfoChuangXiangDTOList.isEmpty()){
            vehicleParkingLot = vehicleParkingLotService.getOne(new LambdaQueryWrapper<VehicleParkingLot>().eq(
                    VehicleParkingLot::getProjectId, project.getId()
            ).eq(
                    VehicleParkingLot::getParkId, vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getParkId()
            ));
            if(vehicleParkingLot == null){
                throw new BusinessException("创享，通过第三方车场id查询不到车场信息！");
            }
            vehicleCardList = vehicleCardService.list(new LambdaQueryWrapper<VehicleCard>().eq(
                    VehicleCard::getProjectId, project.getId()
            ).eq(
                    VehicleCard::getParkingLotId, vehicleParkingLot.getId()
            ));
            for (CarMonthInfoChuangXiangDTO carMonthInfoChuangXiangDTO : carMonthInfoChuangXiangDTOList) {
                VehicleCar vehicleCar = vehicleCarThirdConverter.chuangXiangtoVehicleCar(carMonthInfoChuangXiangDTO, chuangXiangtoCardType(carMonthInfoChuangXiangDTO.getCartypeId(), vehicleCardList),
                        project.getId(), project.getName(), vehicleParkingLot.getId(), vehicleParkingLot.getParkingLotName(), null, defaultAccount, defaultAccount);
                vehicleCarListFromThird.add(vehicleCar);
            }
        }

        List<VehicleCar> vehicleCarListFromCent = vehicleCarService.list(new LambdaQueryWrapper<VehicleCar>().eq(
                VehicleCar::getProjectId, project.getId()
        ).eq(
                Objects.nonNull(vehicleParkingLot), VehicleCar::getParkingLotId, vehicleParkingLot.getId()
        ));

        vehicleCommonBusinessService.syncCar(vehicleCarListFromCent,vehicleCarListFromThird);
        log.info("Chuang Xiang Car sync end!");
    }

    /**
     * 通过第三方创享的车卡id获取中台的车卡id
     */
    private Integer chuangXiangtoCardType(String cartypeId,List<VehicleCard> vehicleCardList){
        VehicleCard vehicleCard = vehicleCardList.stream().filter(s -> {
            if (StringUtils.equals(s.getThirdRuleId(), cartypeId)) {
                return true;
            } else {
                return false;
            }
        }).findAny().orElse(null);
        if(vehicleCard!=null){
            return vehicleCard.getCardType();
        }
        return noCardType;
    }
}
